﻿
using GalaSoft.MvvmLight.Command;
using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;

namespace HuiZe.education.ViewModel
{
    public class PersonCenterViewModel : ViewModelBase
    {
        public int BDType { get; set; }

        public string SjbdStr { get; set; } = "解绑";
        public string QQbdStr { get; set; } = "解绑";
        public string WXbdStr { get; set; } = "解绑";
        public PersonCenterView personCenterView { get; set; }
        public ObservableCollection<ClassModel3> Grades { get; set; } //班级
        public Visibility BandingVisibility { get; set; }
        public Visibility sjbdVisibility { get; set; }
        public Visibility xgmmVisibility { get; set; }


        public Visibility UnBandingVisibility { get; set; }
        public ErrorMsgModel ErrorMsg { get; set; } = new ErrorMsgModel();
        public Visibility UnBandingBoxVisibility { get; set; } = Visibility.Collapsed;
        public RelayCommand<object> FogotPasswordCommand { get { return new RelayCommand<object>(FogotPassword); } } //忘记密码

        public RelayCommand<object> BindCommand { get { return new RelayCommand<object>(Bind); } } //解除绑定
        public RelayCommand<object> RelieveBindCommand { get { return new RelayCommand<object>(RelieveBind); } } //解除绑定
        public string PhoneNo { get; set; }
        public string VerCode { get; set; }
        public string FirstPwd { get; set; }
        public string SecondPwd { get; set; }
        public ResetpwdModel ResetpwdModel { get; set; } = new ResetpwdModel();
        public string strSendValidationCode { get; set; } = "获取验证码";
        public string strSendValidationCode2 { get; set; } = "获取验证码";
        private int timerCount = 60;
        private Userinfo _user;
        public Userinfo user
        {
            get { return _user; }
            set
            {
                _user = value;
                if (!value.gender)
                    manSex = true;
                else
                    womanSex = true;
            }
        }

        public bool manSex { get; set; }
        public bool womanSex { get; set; }

        public RelayCommand UnBanding
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    //PhoneNo = "15255472974";
                    UnBandingBoxVisibility = Visibility.Visible;
                }));
            }
        }
        public RelayCommand UnBandingCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    //PhoneNo = "15255472974";
                    UnBandingBoxVisibility = Visibility.Visible;
                }));
            }
        }
        public RelayCommand HideBox
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    UnBandingBoxVisibility = Visibility.Collapsed;
                }));
            }
        }
        public ObservableCollection<CategoryModel3> selectedCategory { get; set; }
        public ClassModel3 selectedClass { get; set; }
        public ObservableCollection<ClassModel3> selectedClass2 { get; set; }
        public ObservableCollection<CategoryModel3> Categorys { get; set; }
        public ObservableCollection<TreeData> classData { get; set; }


        //找回密码
        public PersonCenterViewModel()
        {
            if (IsInDesignMode)
            {
                BandingVisibility = Visibility.Visible;
                UnBandingVisibility = Visibility.Collapsed;
                // user = new Userinfo() { id = 123, mobile = "15903613610", nickname = "测试" };
                user = HuiZeeducation.Instance.UserInfo;
                manSex = true;
            }
            BandingVisibility = Visibility.Visible;
            UnBandingVisibility = Visibility.Collapsed;
           

        }
        public void LoadData()
        {
            user = HuiZeeducation.Instance.UserInfo;
            user.expiretime2 = StampToDateTime(user.expiretime.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            PhoneNo = user.mobile;

            Grades = new ObservableCollection<ClassModel3>(DownLoadClassData());
            classData = new ObservableCollection<TreeData>();
            //if (HuiZeeducation.Instance.Classes != null)
            //{
            //    foreach (var item in HuiZeeducation.Instance.Classes)
            //    {
            //        TreeData data = new TreeData()
            //        {
            //            id = item.id,
            //            DisplayName = item.name,
            //            Children = new List<TreeData>()
            //        };
            //        foreach (var subClass in item.list)
            //        {
            //            data.Children.Add(new TreeData()
            //            {
            //                id = subClass.id,
            //                DisplayName = subClass.name
            //            });
            //        }
            //        classData.Add(data);
            //    }
            //}
            if (Grades != null)
            {
                foreach (var item in Grades)
                {
                    TreeData data = new TreeData()
                    {
                        id = item.id,
                        DisplayName = item.name,
                        Children = new List<TreeData>()
                    };
                    List<TreeData> child = new List<TreeData>();
                    foreach (var subClass in item.list)
                    {
                        //var childdata = new TreeData()
                        //{
                        //    id = subClass.id,
                        //    DisplayName = subClass.name,
                        //    Children = new List<TreeData>()
                        //};

                        data.Children.Add(new TreeData
                        {
                            id = subClass.id,
                            DisplayName = subClass.use_name
                        });

                        // foreach (var subClass0 in subClass.list)
                        //{
                        //     data.Children.Add(new TreeData 
                        //     {
                        //         id = subClass0.id,
                        //         DisplayName = subClass0.use_name

                        //     });

                        //}

                        // data.Children.Add(data);


                    }
                    classData.Add(data);
                }
            }


            GetLoginInfo();
            DownLoadCategory();
            //Categorys = new ObservableCollection<CategoryModel2>(HuiZeeducation.Instance.Categorys);//科目
            selectedCategory = new ObservableCollection<CategoryModel3>(Categorys.Where(o => o.id == user.subject).ToList());

            //selectedCategory = Categorys.FirstOrDefault(o=>o.id==user.subject);

            selectedClass2 = new ObservableCollection<ClassModel3>(Grades.Where(o => o.id == user.grades_id).ToList());
            //HuiZeeducation.Instance.UserInfo
            SelectClass(classData.FirstOrDefault().Children.FirstOrDefault());

             //userclassid 
             //usergrades 
            foreach (var childitem0 in classList)
            {
                foreach (var childitem1 in childitem0.list)
                {
                    

                }
            }


        }

        List<ClassModel3> classList = new List<ClassModel3>();
        string userclassid = string.Empty;
        string usergrades = string.Empty;

        public List<ClassModel3> DownLoadClassData()
        {
            ReqClassInfoModel reqClassInfoModel = new ReqClassInfoModel()
            {
                token = user.token,

            };
            string data = JsonHelper.ToJson(reqClassInfoModel);
            var result = Request<List<ClassModel3>>("/api/grades/getGradesList", data, "");
            if (result.Item1)
            {
                classList= result.Item2.data;
                return result.Item2.data;
            }
            else
                return null;
        }

        public void DownLoadCategory()
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs["token"] = user.token;

            string data = JsonHelper.ToJson(keyValuePairs);
            var result = Request<List<CategoryModel3>>("/api/grades_subject/index", data, "获取任课科目");
            if (result.Item1)
            {
                Categorys = new ObservableCollection<CategoryModel3>(result.Item2.data);
            }
        }


        public static DateTime StampToDateTime(string stamp)
        {
            if (stamp.Length != 10 || stamp.Length != 13) return DateTime.Now;
            try
            {
                DateTime StartDateTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
                if (stamp.Length == 10) StartDateTime.AddSeconds(long.Parse(stamp));
                return StartDateTime.AddMilliseconds(long.Parse(stamp));
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }
        private void Timer_Tick(object sender, System.EventArgs e)
        {
            if (timerCount == 0)
            {
                timer2.Enabled = false;
                timer2.Stop();
                timerCount = 60;
                strSendValidationCode = "获取验证码";
            }
            else
            {
                timerCount--;
                strSendValidationCode = $"{timerCount}s";
            }
        }
        private void Timer_Tick2(object sender, System.EventArgs e)
        {
            if (timerCount == 0)
            {
                timer.Enabled = false;
                timer.Stop();
                timerCount = 60;
                strSendValidationCode2 = "获取验证码";
            }
            else
            {
                timerCount--;
                strSendValidationCode2 = $"{timerCount}s";
            }
        }
        Timer timer2;
        private void FogotPassword(object obj)
        {
            ErrorMsg = new ErrorMsgModel();
            if (obj.ToString() == "3") //发送验证码
            {
                if (timerCount < 60) return;
                if (string.IsNullOrEmpty(PhoneNo))
                {
                    ErrorMsg.ForgotMobileError = "手机号不能为空";
                    return;
                }
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
                keyValuePairs["mobile"] = PhoneNo;
                keyValuePairs["event"] = "resetpwd";
                string data = JsonHelper.ToJson(keyValuePairs);
                var res = WebApiHelper.HttpPost("/api/sms/send", data);
                if (res.Item1)
                {
                    var request = JsonHelper.FromJson<RequestModel<ReqLoginModel>>(res.Item2);
                    if (request.code != 1)
                    {
                        ErrorMsg.ForgotValidationCodeError = request.msg;
                        return;
                    }
                    timer2 = new Timer()
                    {
                        Interval = 1000,
                    };
                    timer2.Tick += Timer_Tick;
                    timer2.Enabled = true;
                    timer2.Start();
                }
                else
                {
                    ShowDialogView("验证码发送失败！");
                    return;
                }
            }

            else if (obj.ToString() == "4")//修改并登录
            {
                if (timer2 != null)
                {
                    timer2.Enabled = false;
                    timer2.Stop();
                }
                timerCount = 60;
                strSendValidationCode = "获取验证码";


                if (string.IsNullOrEmpty(PhoneNo))
                {
                    ErrorMsg.ForgotMobileError = "手机号不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(ResetpwdModel.captcha))
                {
                    ErrorMsg.ForgotValidationCodeError = "验证码不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(FirstPwd))
                {
                    ErrorMsg.ForgotPwdError = "密码不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(SecondPwd))
                {
                    ErrorMsg.ForgotSecondPwdError = "请输入确认密码";
                    return;
                }
                if (FirstPwd.Length < 6)
                {
                    ErrorMsg.ForgotPwdError = "密码至少6个字符";
                    return;
                }
                if (SecondPwd.Length < 6)
                {
                    ErrorMsg.ForgotSecondPwdError = "确认密码至少6个字符";
                    return;
                }
                if (FirstPwd != SecondPwd)
                {
                    ErrorMsg.ForgotSecondPwdError = "两次密码输入不一致";
                    return;
                }
                ResetpwdModel.mobile = PhoneNo;
                ResetpwdModel.captcha = ResetpwdModel.captcha;
                ResetpwdModel.newpassword = FirstPwd;
                ResetpwdModel.type = "mobile";
                string data = JsonHelper.ToJson(ResetpwdModel);
                var res = WebApiHelper.HttpPost("/api/user/resetpwd", data);
                if (res.Item1)
                {
                    var request = JsonHelper.FromJson<RequestModel<ReqLoginModel>>(res.Item2);
                    if (request.code == 1) //修改成功 直接登陆
                    {
                        xgmmVisibility = Visibility.Collapsed;
                        LoginIn(PhoneNo, FirstPwd);
                        //GetLoginInfo();
                        //ShowDialogView(request.msg);
                    }
                    else
                    {
                        //ShowDialogView(request.msg);
                        ErrorMsg.ForgotSecondPwdError = request.msg;
                        return;
                    }
                }
                else
                {
                    ShowDialogView("重置密码失败！");
                    return;
                }
            }
        }
        Timer timer;
        private void RelieveBind(object obj)
        {

            if (obj.ToString() == "3") //发送验证码
            {
                if (timerCount < 60) return;
                if (string.IsNullOrEmpty(PhoneNo))
                {
                    ErrorMsg.ForgotMobileError = "手机号不能为空";
                    return;
                }
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
                keyValuePairs["mobile"] = PhoneNo;
                keyValuePairs["event"] = "relievemobile";
                string data = JsonHelper.ToJson(keyValuePairs);
                var res = WebApiHelper.HttpPost("/api/sms/send", data);
                if (res.Item1)
                {
                    var request = JsonHelper.FromJson<RequestModel<ReqLoginModel>>(res.Item2);
                    if (request.code != 1)
                    {
                        ErrorMsg.ForgotValidationCodeError = request.msg;
                        return;
                    }
                    timer = new Timer()
                    {
                        Interval = 1000,
                    };
                    timer.Tick += Timer_Tick2;
                    timer.Enabled = true;
                    timer.Start();
                }
                else
                {
                    ShowDialogView("验证码发送失败！");
                    return;
                }
            }

            else if (obj.ToString() == "4")//修改并登录
            {

                if (timer != null)
                {
                    timer.Enabled = false;
                    timer.Stop();
                }
                timerCount = 60;
                strSendValidationCode2 = "获取验证码";

                if (string.IsNullOrEmpty(PhoneNo))
                {
                    ErrorMsg.ForgotMobileError = "手机号不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(VerCode))
                {
                    ErrorMsg.ForgotValidationCodeError = "验证码不能为空";
                    return;
                }
                RelieveModel relieveModel = new RelieveModel
                {
                    token = HuiZeeducation.Instance.UserInfo.token,
                    mobile = PhoneNo,
                    captcha = VerCode,
                };
                if (BDType == 1)
                {
                    relieveModel.type = "mobile";
                }
                else if (BDType == 2)
                {
                    relieveModel.type = "QQ";
                }
                else
                {
                    relieveModel.type = "wechat";
                }
                string data = JsonHelper.ToJson(relieveModel);
                var res = WebApiHelper.HttpPost("/api/user/relieve", data);
                if (res.Item1)
                {
                    var request = JsonHelper.FromJson<RequestModel<ReqLoginModel>>(res.Item2);
                    if (request.code == 1) //修改成功 直接登陆
                    {
                        UnBandingVisibility = Visibility.Collapsed;
                        if (BDType == 1)
                            SjbdStr = "绑定";
                        else if (BDType == 2)
                            QQbdStr = "绑定";
                        else if (BDType == 3)
                            WXbdStr = "绑定";
                        GetLoginInfo();
                        ShowDialogView(request.msg);
                    }
                    else
                    {
                        ShowDialogView(request.msg);
                        ErrorMsg.ForgotSecondPwdError = request.msg;
                        return;
                    }
                }
                else
                {
                    ShowDialogView("解绑失败！");
                    return;
                }
            }
        }


        private void Bind(object obj)
        {

            if (obj.ToString() == "3") //发送验证码
            {
                if (timerCount < 60) return;
                if (string.IsNullOrEmpty(PhoneNo))
                {
                    ErrorMsg.ForgotMobileError = "手机号不能为空";
                    return;
                }
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
                keyValuePairs["mobile"] = PhoneNo;
                keyValuePairs["event"] = "binding";
                string data = JsonHelper.ToJson(keyValuePairs);
                var res = WebApiHelper.HttpPost("/api/sms/send", data);
                if (res.Item1)
                {
                    var request = JsonHelper.FromJson<RequestModel<ReqLoginModel>>(res.Item2);
                    if (request.code != 1)
                    {
                        ErrorMsg.ForgotValidationCodeError = request.msg;
                        return;
                    }
                    timer = new Timer()
                    {
                        Interval = 1000,
                    };
                    timer.Tick += Timer_Tick2;
                    timer.Enabled = true;
                    timer.Start();
                }
                else
                {
                    ShowDialogView("验证码发送失败！");
                    return;
                }
            }

            else if (obj.ToString() == "4")//修改并登录
            {

                if (timer != null)
                {
                    timer.Enabled = false;
                    timer.Stop();
                }
                timerCount = 60;
                strSendValidationCode2 = "获取验证码";

                if (string.IsNullOrEmpty(PhoneNo))
                {
                    ErrorMsg.ForgotMobileError = "手机号不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(VerCode))
                {
                    ErrorMsg.ForgotValidationCodeError = "验证码不能为空";
                    return;
                }
                BindModel relieveModel = new BindModel
                {
                    id = HuiZeeducation.Instance.UserInfo.id,
                    mobile = PhoneNo,
                    code = VerCode,
                    token = HuiZeeducation.Instance.UserInfo.token,
                };
                if (BDType == 0)
                {
                    relieveModel.type = "mobile";
                }

                string data = JsonHelper.ToJson(relieveModel);
                var res = WebApiHelper.HttpPost("/api/user/binging", data);
                if (res.Item1)
                {
                    var request = JsonHelper.FromJson<RequestModel<ReqLoginModel>>(res.Item2);
                    if (request.code == 1) //修改成功 直接登陆
                    {
                        //if (BDType == 1)
                        SjbdStr = "解绑";
                        //else if (BDType == 2)
                        //    QQbdStr = "解绑";
                        //else if (BDType == 3)
                        //    WXbdStr = "解绑";

                        sjbdVisibility = Visibility.Collapsed;
                        GetLoginInfo();
                        ShowDialogView(request.msg);
                    }
                    else
                    {
                        ShowDialogView(request.msg);
                        ErrorMsg.ForgotSecondPwdError = request.msg;
                        return;
                    }
                }
                else
                {
                    ShowDialogView("绑定失败！");
                    return;
                }
            }
        }


        public void SelectClass(object obj)
        {
            var item = obj as TreeData;

            foreach (var childitem0 in classList)
            {
                foreach (var childitem1 in childitem0.list)
                {
                    if (childitem1.id == item.id)
                    {

                        Grades = new ObservableCollection<ClassModel3>(childitem1.list);

                        break;
                    }

                }
            }
            
            //if (item.Children.Count == 0)
            //{
            //    var list = HuiZeeducation.Instance.DownLoadClassData2(item.id, "获取班级列表");
            //    if (list != null)
            //    {
            //        Grades = new ObservableCollection<ClassModel3>(list);
            //    }
            //}

            foreach (var subitem in Grades)
            {
                subitem.name = subitem.use_name;
            }
        }


        public RelayCommand SaveCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    SaveData();
                }));
            }
        }

        public void SaveData()
        {
            var list = personCenterView.KMComBox.selectedCheckList;
            var list2 = personCenterView.treeView.ClassModellist;

            if (list == null)
            {
                ShowDialogView("请选择授课科目!");
                return;
            }
            if (list2 == null)
            {
                ShowDialogView("请选择授课班级!");
                return;
            }
            string Categoryid = ""; ;
            foreach (var item in list)
            {
                if (string.IsNullOrWhiteSpace(Categoryid))
                {
                    Categoryid += item.id;
                }
                else
                {
                    Categoryid += "," + item.id;
                }
            }
            Dictionary<string, string> GradeDic = new Dictionary<string, string>();
            List<int> GradeIdList = new List<int>();
            string GradesStr = ""; ;
            foreach (var item in list2)
            {
                if (string.IsNullOrWhiteSpace(GradesStr))
                {

                    GradesStr += item.pid + ",";
                    GradeDic.Add(item.pid.ToString(), item.pid.ToString());
                    GradeIdList.Add(item.pid);
                }
                else
                {
                    if (!GradeDic.ContainsKey(item.pid.ToString()))
                    {
                        GradeDic.Add(item.pid.ToString(), item.pid.ToString());
                        GradeIdList.Add(item.pid);
                        GradesStr += item.pid + ",";
                    }
                }
            }
            string ClassStr = "[";

            for (int i = 0; i < GradeIdList.Count; i++)
            {
                int pid = GradeIdList[i];

                var list11 = list2.Where(o => o.pid == pid).ToList();
                string childClassStr = "[";
                for (int j = 0; j < list11.Count; j++)
                {
                    if (j != list11.Count - 1)
                        childClassStr += list11[j].id.ToString() + ",";
                    else
                        childClassStr += list11[j].id.ToString();
                }
                if (i != GradeIdList.Count - 1)
                    childClassStr += "],";
                else
                    childClassStr += "]";
                ClassStr += childClassStr;
            }
            ClassStr += "]";


            ReqEditPersonModel reqClassInfoModel = new ReqEditPersonModel()
            {

                token = HuiZeeducation.Instance.UserInfo.token,
                grades_ids = GradesStr == "" ? "" : GradesStr.Substring(0, GradesStr.Length - 1),
                class_ids = ClassStr,
                subject_id = Categoryid,
            };
            string data = JsonHelper.ToJson(reqClassInfoModel);
            var result = Request<ResEditPersonModel>("/api/user/edituser", data, "修改个人资料");
            if (result.Item1)
            {
                ShowDialogView("修改成功");
                GetLoginInfo();
            }
            else
            {

            }


        }

        public string WXLogin()
        {
            string url = WXLoginUrl(1);
            //string newurl = url;
            //int index = url.IndexOf("d-zhzh.bigchun.com");
            //string preurl = url.Substring(0, index);
            //string nexturl = url.Substring(index + 18, url.Length - index - 18);
            //newurl = preurl + "admin.jiaodehuiyun.com" + nexturl;
            return url;
        }

        public string QQLogin()
        {

            string url = QQLoginUrl(1);
            return url;

        }
        /// <summary>
        /// PC扫码QQ登录地址
        /// </summary>
        /// <param name="Type"></param>
        public string QQLoginUrl(int Type)
        {
            ReqLoginUrl model = new ReqLoginUrl
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                type = Type,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request2<ResLoginUrl>("/api/user/pc_qq_login_url", data, "第三方登陆");
            if (result.Item1)
            {
                return result.Item2.data;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// PC扫码WX登录地址
        /// </summary>
        /// <param name="Type"></param>
        public string WXLoginUrl(int Type)
        {
            ReqLoginUrl model = new ReqLoginUrl
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                type = Type,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request2<ResLoginUrl>("/api/user/pc_third_login_url", data, "第三方登陆");
            if (result.Item1)
            {
                return result.Item2.data;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 绑定第三方
        /// </summary>
        /// <param name="Type"></param>
        public string OtherBinding(int Type)
        {
            ReqLoginUrl model = new ReqLoginUrl
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                type = Type,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request2<ResLoginUrl>("/api/user/binging", data, "绑定第三方");
            if (result.Item1)
            {
                return result.Item2.data;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// PC扫码QQ登录
        /// </summary>
        /// <param name="Type"></param>
        public void QQLogin(string Code, string State)
        {
            ReqOtherLogin model = new ReqOtherLogin
            {
                code = Code,
                state = State,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResLoginDeatail>("/api/user/pc_qq_login", data, "第三方登陆");
            if (result.Item1)
            {
                QQbdStr = "解绑";
                GetLoginInfo();

            }
            else
            {

            }
        }


        /// <summary>
        /// PC扫码WX登录
        /// </summary>
        /// <param name="Type"></param>
        public void WXLogin(string Code, string State)
        {
            ReqOtherLogin model = new ReqOtherLogin
            {
                code = Code,
                state = State,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResLoginDeatail>("/api/user/pc_third_login", data, "第三方登陆");
            if (result.Item1)
            {
                WXbdStr = "解绑";
                GetLoginInfo();
                ////LoginView.Hide();
                //HuiZeeducation.Instance.UserInfo = result.Item2.data.userinfo;
                ////Debug.WriteLine($"登陆成功token：{result.Item2.data.userinfo.token}");
                //HuiZeeducation.Instance.DownLoadData();
            }
            else
            {

            }
        }

        private void LoginIn(string u, string p)
        {
            LoginModel loginModel = new LoginModel
            {
                LoginName = u,
                LoginPassWord = p,


            };
            string data = JsonHelper.ToJson(loginModel);
            var result = Request<ReqLoginModel>("/api/user/login", data, "登陆");
            if (result.Item1)
            {


                HuiZeeducation.Instance.UserInfo = result.Item2.data.userInfo;
                Debug.WriteLine($"登陆成功token：{result.Item2.data.userInfo.token}");
                HuiZeeducation.Instance.DownLoadData();
                HuiZeeducation.Instance.UserInfo.userName = loginModel.LoginName;
                HuiZeeducation.Instance.UserInfo.userPwd = loginModel.LoginPassWord;


            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }
        }
        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        /// <param name="Type"></param>
        public void GetLoginInfo()
        {
            //LoginModel loginModel = new LoginModel
            //{
            //    LoginName = HuiZeeducation.Instance.UserInfo.userName,
            //    LoginPassWord = HuiZeeducation.Instance.UserInfo.userPwd
            //};
            //string data1 = JsonHelper.ToJson(loginModel);
            //var result1 = Request<ReqLoginModel>("/api/user/login", data1, "登陆");
            //if (result1.Item1)
            //{

            //    HuiZeeducation.Instance.UserInfo = result1.Item2.data.userInfo;

            //    HuiZeeducation.Instance.DownLoadData2();
            //    user = HuiZeeducation.Instance.UserInfo;
            //}
            //else
            //{
            //    if (result1.Item2 != null)
            //        ShowDialogView(result1.Item2.msg);
            //}

            ReqLoginUrl model = new ReqLoginUrl
            {
                token = HuiZeeducation.Instance.UserInfo.token,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<Userinfo>("/api/user/getuser", data, "获取登录用户信息");
            if (result.Item1)
            {
                //user = new Userinfo
                //{
                //      id= result.Item2.data.id,
                //     username = result.Item2.data.username,
                //    nickname = result.Item2.data.nickname,
                //    mobile = result.Item2.data.mobile,
                //    avatar = result.Item2.data.avatar,
                //    score = result.Item2.data.score,
                //    token = result.Item2.data.token,
                //    qq = result.Item2.data.qq,
                //    wx = result.Item2.data.wx,
                //    school_name = result.Item2.data.school_name,

                //};
                //user.username = result.Item2.data.username;
                //user.nickname = result.Item2.data.nickname;
                //user.mobile = result.Item2.data.mobile;
                //user.avatar = result.Item2.data.avatar;
                //user.score = result.Item2.data.score;

                //user.grades_ids = result.Item2.data.grades_ids;
                //user.subject_id = result.Item2.data.subject_id;
                //user.class_ids = result.Item2.data.class_ids;
                //user.qq = result.Item2.data.qq;
                //user.wx = result.Item2.data.wx;
                //user.school_name = result.Item2.data.school_name;

                string token = HuiZeeducation.Instance.UserInfo.token;
                string expireTime = HuiZeeducation.Instance.UserInfo.expiretime2;
                HuiZeeducation.Instance.UserInfo = result.Item2.data;

                HuiZeeducation.Instance.UserInfo.token = token;
                HuiZeeducation.Instance.UserInfo.logoUrl = GetLoginLogo();
                HuiZeeducation.Instance.UserInfo.expiretime2 = expireTime;


                user = HuiZeeducation.Instance.UserInfo;

                 userclassid = user.class_ids;
                 usergrades = user.grades_ids;


                //HuiZeeducation.Instance.DownLoadData2();

            }
            else
            {

            }
        }


     
        private string GetLoginLogo()
        {
            ReqLoginUrl model = new ReqLoginUrl
            {

                token = HuiZeeducation.Instance.UserInfo.token,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ReqLoginLogo>("/api/user/getLogo", data, "getLogo");
            if (result.Item1)
            {
                return result.Item2.data.image;
                //HuiZeeducation.Instance.UserInfo.logoUrl = result.Item2.data.image;
                //UserInfo = HuiZeeducation.Instance.UserInfo;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return "";
            }
        }

        public RelayCommand UploadCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    userUploads();
                }));
            }



        }
        public void userUploads()
        {
            string filepath = "";
            string filename = "";
            string filesize = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件";
            dialog.Filter = "*|*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                filepath = dialog.FileName;
                filename = dialog.SafeFileName;
                FileInfo fileInfo = new FileInfo(dialog.FileName);
                filesize = (fileInfo.Length / 1024).ToString();

            }
            if (string.IsNullOrWhiteSpace(filename))
            {
                return;
            }

            ReqUploadModel model = new ReqUploadModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;
            string data2 = JsonHelper.ToJson(model);
            var result2 = Upload<ResUploadModel>("/api/common/oss_upload", data2, "上传文件", filepath);
            if (result2.Item1)
            {
                string url = result2.Item2.data.url;
                ReqEditPersonModel reqClassInfoModel = new ReqEditPersonModel()
                {
                    token = HuiZeeducation.Instance.UserInfo.token,
                    avatar = url,
                };
                string data = JsonHelper.ToJson(reqClassInfoModel);
                var result = Request<ResEditPersonModel>("/api/user/edituser", data, "修改个人资料");
                if (result.Item1)
                {
                    ShowDialogView(result.Item2.msg);
                    //LoginModel loginModel = new LoginModel
                    //{
                    //    LoginName = HuiZeeducation.Instance.UserInfo.userName,
                    //    LoginPassWord= HuiZeeducation.Instance.UserInfo.userPwd
                    //};
                    //string data1 = JsonHelper.ToJson(loginModel);
                    //var result1 = Request<ReqLoginModel>("/api/user/login", data1, "登陆");
                    //if (result1.Item1)
                    //{

                    //    HuiZeeducation.Instance.UserInfo = result1.Item2.data.userInfo;

                    //    HuiZeeducation.Instance.DownLoadData();
                    //    user = HuiZeeducation.Instance.UserInfo;
                    //}
                    //else
                    //{
                    //    if (result1.Item2 != null)
                    //        ShowDialogView(result1.Item2.msg);
                    //}
                    GetLoginInfo();
                }
                else
                {
                    ShowDialogView(result.Item2.msg);
                }
            }
            else
            {
                ShowDialogView(result2.Item2.msg);
                return;
            }


        }


    }
}
